$(function(){
    //初始化页面列表
    $("#accident_datagrid").datagrid({
        fit:true,
        url:"/accident/list",
        fitColumns:true,
        striped:true,
        pagination:true,
        singleSelect:true,
        checkOnSelect:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',title:'编号', hidden:true},
            {field:'checkbox',checkbox:true,tilte:'选中',width:100},
            {field:'state',title:'当前状态',align:'center',width:100,formatter:function(value){
                if(value==1){
                    return "<p style='color: orange'>待验证</p>"
                }else if(value==2){
                    return "<p style='color: hotpink'>验证完,待填理赔单</p>"
                }else if(value==3){
                    return "<p style='color: mediumpurple'>理赔单完,待提审</p>"
                }else if(value==4){
                    return "<p style='color: #00bbee'>待审核</p>"
                }else if(value==5){
                    return "<p style='color: green'>审核完成</p>"
                }else if(value==6){
                    return "<p style='color: greenyellow'>支付完成</p>"
                }else if(value==7){
                    return "<p style='color: red'>支付失败</p>"
                }else if(value==8){
                    return "<p style='color: red'>审核驳回</p>"
                }else{
                    return "<p style='color: deepskyblue'>未知状态</p>"
                }
            }},
            {field:'reporterName',title:'报案人姓名',align:'center',width:100,formatter:function(value,rows,index){
                if(rows.client!=null){
                    return rows.client.clientname;
                }else{
                    return "";
                }
            }},
            {field:'gender',title:'性别',align:'center',width:100,formatter:function(value,rows,index){
                if(rows.client.gender==1){
                    return "男";
                }else{
                    return "女";
                }
            }},
            {field:'tel',title:'联系方式',align:'center',width:100,formatter:function(value,rows,index){
                if(rows.client!=null){
                    return rows.client.tel;
                }else{
                    return "";
                }
            }},
            {field:'email',title:'邮箱',align:'center',width:100,formatter:function(value,rows,index){
                if(rows.client!=null) {
                    return rows.client.email;
                }else{
                    return "";
                }
            }},
            {field:'occurenceTime',title:'事故发生时间',align:'center',width:100},
            {field:'occurencePlace',title:'事故发生地点',align:'center',width:100},
            {field:'insuranceNo',title:'保险单号',align:'center',width:100},
            {field:'inputStaff',title:'录单人',align:'center',width:100}
        ]]
    })
    //初始化页面列表--end

    //初始化弹窗
    $("#accident_dialog").dialog({
        width:400,
        height:400,
        buttons:"#bb",
        closed:true
    })
    //初始化弹窗--end


})

//页面列表添加事件
function add(){
        //初始化新增/编辑下拉表格
        $("#client_grid").combogrid({
            mode:'remote',
            panelWidth:450,
            idField:'insuranceNo',
            textField:'clientname',
            fit:true,
            fitColumns:true,
            striped:true,
            singleSelect:true,
            checkOnSelect:true,
            url:'/client/queryFormalClient',
            pagination:true,
            rownumbers:true,
            toolbar:"#accident_tb",
            columns:[[
                {field:'id',title:'客户编号',width:60,hidden:true},
                {field:'clientname',title:'客户姓名',width:100,align:'center'},
                {field: 'gender', title: '性别', width: 100, align: 'center', formatter: function (value) {
                        if (value == 1) {
                            return "男";
                        } else {
                            return "女";
                        }
                    }
                },
                {field:'tel',title:'联系方式',width:100,align:'center'},
                {field:'email',title:'邮箱',width:100,align:'center'},
                {field:'insuranceNo',title:'保险单号',width:100,align:'center'}
            ]]
        })
        //初始化新增/编辑下拉表格--end

        $("#accident_form").form("clear");
        $("#accident_dialog").dialog("open");
        $("#accident_dialog").dialog("setTitle","<p style='text-align: center;color:red;font-size: 13px;line-height:8px'>新增事故备案</p>");
    }
//页面列表添加事件--end

//弹窗底部保存按钮
function save(){
    var row  = $("#client_grid").combogrid("grid").datagrid("getSelected")
    var id = $("#accidentId").val();
    var url = "/accident/save?insuranceNO="+row.insuranceNO
    if(id){
        url="/accident/update?insuranceNO="+row.insuranceNO
    }
    $("#accident_form").form("submit",{
        url:url,
       /* onSubmit: function(){
            insuranceNO:row.insuranceNO
        },*/
        success:function(data){
            data=$.parseJSON(data);

            if(data.success){
                $("#accident_datagrid").datagrid("load");
                $("#accident_dialog").dialog("close");
            }else{
                $.messager.alert("温馨提示",data.msg)
            }
        }
    })



}
//弹窗底部保存按钮--end

//列表页面编辑事件
function edit(){
    var selectedRow = $("#accident_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要编辑的数据");
        return;
    }
    $("#client_grid").combogrid("setValue",selectedRow.client.id)
    $("#accident_dialog").dialog("setTitle","<p style='text-align: center;color:red;font-size: 13px;line-height:8px'>编辑事故备案</p>");
    $("#accident_form").form("clear");
    $("#accident_dialog").dialog("open");
    selectedRow["client.id"]=selectedRow.client.id;
    $("#accident_form").form("load",selectedRow);
}
//列表页面编辑事件--end


//列表页面删除事件
function remove(){
    var selectedRow = $("#accident_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要删除的数据");
        return;
    }
    $.messager.confirm('确认','您确认想要删除这条数据吗？',function(r){
        if (r){
            $.get("/accident/deleteAccident?id="+selectedRow.id,function(data){
                if(data.success){
                    $("#accident_datagrid").datagrid("load");
                }else{
                    $.messager.alert("温馨提示",data.msg)
                }
            })

        }
    });
}
//列表页面删除事件--end

//弹窗底部关闭按钮
function closes(){
    $("#accident_dialog").dialog("close")
}
//弹窗底部关闭按钮--end


//添加/编辑客户查询
function searchClient() {
    var keyword = $("#clientId").textbox("getValue");
    $("#client_grid").combogrid("grid").datagrid('load',{keywords:keyword})
}
//添加/编辑客户查询--end

//刷新列表页面
function reload1(){
    $("#accident_datagrid").datagrid("reload");
}
//刷新列表页面--end

//提交验证
function validate() {
    var selectedRow = $("#accident_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要验证的数据");
        return;
    }else{
        $.messager.confirm('确认','您确认想要验证这条数据吗？',function(r){
            if (r){
                $.get("/claimBill/updateInvalidateState?id="+selectedRow.id,function(data){
                    if(data.success){
                        $("#accident_datagrid").datagrid("load");
                        $("#accident_dialog").dialog("close");
                    }else{
                        $.messager.alert("温馨提示",data.msg)
                    }
                })

            }
        });
    }

}
//提交验证--end